/*
 * Copyright (C) 2014 naBerTech snc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.nabertech.example.android.wearable.sendphoto;

import android.app.Notification;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.util.Log;

import com.example.android.wearable.sendphoto.R;
import com.google.android.gms.wearable.Node;
import com.nabertech.example.android.wearable.sendphoto.common.Constants;
import com.google.android.gms.wearable.MessageEvent;
import com.google.android.gms.wearable.WearableListenerService;

/**
 * Waiting for SendPhoto Messages.
 */
public class PhotoListenerService extends WearableListenerService {

    private static final String TAG = "PhotoListenerService";
    private static int ID_PRESENTATION = 10;

    @Override
    public void onMessageReceived(MessageEvent messageEvent) {
        Log.d(TAG, "onMessageReceived: " + messageEvent);

        // Check to see if the message is to notify a new photo
        if (messageEvent.getPath().equals(Constants.NOTIFY_NEW_PHOTO)) {
            postNotification(messageEvent);
        }
    }

    @Override
    public void onPeerConnected(Node peer) {
        Log.d(TAG, "onPeerConnected: " + peer);
    }

    @Override
    public void onPeerDisconnected(Node peer) {
        Log.d(TAG, "onPeerDisconnected: " + peer);
    }

    private void postNotification(MessageEvent messageEvent) {

        Intent startIntent = new Intent(this, ShowActivity.class);
        startIntent.putExtra(ShowActivity.SOURCE_NODE_ID, messageEvent.getSourceNodeId());
        PendingIntent startPendingIntent =
                PendingIntent.getActivity(getApplicationContext(), 0, startIntent,
                        PendingIntent.FLAG_CANCEL_CURRENT);

        NotificationCompat.Action action =
                new NotificationCompat.Action.Builder(R.drawable.ic_launcher,
                        getString(R.string.action_label), startPendingIntent)
                        .build();

        NotificationCompat.WearableExtender extender = new NotificationCompat.WearableExtender();
        extender.addAction(action);

        NotificationCompat.Builder builder =
                new NotificationCompat.Builder(getApplicationContext())
                        .setContentTitle(getString(R.string.notification_title))
                        .setContentText(getString(R.string.notification_content))
                        .setSmallIcon(R.drawable.ic_launcher);

        builder.extend(extender);
        Notification notification = builder.build();

        NotificationManagerCompat notificationManager =
                NotificationManagerCompat.from(getApplicationContext());

        notificationManager.notify(ID_PRESENTATION, notification);
    }
}
